import Vue from 'vue'
import VueRouter from 'vue-router'
// 一级路由
import DetailDe from '@/views/DetailDe'
import LoginLo from '@/views/LoginLo'
import LayOut from '@/views/LayOut'
import RegisterRe from '@/views/RegisterRe'
// 二级路由
import HomePage from '@/views/HomePage'
import FindRoom from '@/views/FindRoom'
import InforMation from '@/views/InforMation'
import MyRoom from '@/views/MyRoom'
import { getToken } from '@/utils/storage'

import MapMap from '@/views/MapMap'
import CollEct from '@/views/CollEct'
import MyLet from '@/views/MyLet'
import ToLet from '@/views/ToLet'
import CityList from '@/views/CityList'
Vue.use(VueRouter)

// const routes = [

// ]

const router = new VueRouter({
  // routes
  routes: [
    {
      path: '/detailde',
      component: DetailDe
    },
    {
      path: '/collect',
      component: CollEct
    },
    {
      path: '/mylet',
      component: MyLet
    },
    {
      path: '/tolet',
      component: ToLet
    },
    {
      path: '/citylist',
      component: CityList
    },
    {
      path: '/map',
      component: MapMap
    },
    {
      path: '/loginlo',
      component: LoginLo
    },
    {
      path: '/registerre',
      component: RegisterRe
    },
    {
      path: '/',
      component: LayOut,
      redirect: '/homepage',
      children: [
        {
          path: '/homepage',
          component: HomePage
        },
        {
          path: '/findroom',
          component: FindRoom
        },
        {
          path: '/information',
          component: InforMation
        },
        {
          path: '/myroom',
          component: MyRoom
        }
      ]

    }

  ]

})
// 设置白名单是否跳转
const whiteList = ['/loginlo', '/registerre', '/myroom', '/collect',
  '/detailde', '/homepage', '/map', '/findroom', '/information', '/mylet', '/tolet',
  '/citylist']
router.beforeEach((to, from, next) => {
  // to去哪里 from从哪里来 next是一个函数
  // 只有调用next 才会正常放行，next(path)可以跳到指定的路径
  // console.log(to, from)
  const token = getToken()
  // 如果有token直接放行
  if (token) {
    next()
  } else {
    // 如果没有token就检测是否去的是白名单，如果是 直接放行
    if (whiteList.includes(to.path)) {
      next()
    } else {
      // 否则就让去登录页面
      next('/loginlo')
    }
  }
})

export default router
